package com.neusoft.neuiotms.dcam.fixed.converter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.constant.DCAMConsts;
import com.neusoft.neuiotms.dcam.fixed.dto.StockWarningDTO;
import com.neusoft.neuiotms.dcam.fixed.model.AssetType;
import com.neusoft.neuiotms.dcam.fixed.model.StockWarning;
import com.neusoft.neuiotms.dcam.fixed.repository.StockWarningRepository;

@Component
public class StockWarningConverter extends AbstractConverter<StockWarning, StockWarningDTO> {

    @Autowired
    private StockWarningRepository stockWarningRepository;
    @Autowired
    private AssetTypeConverter assetTypeConverter;

    @Override
    public StockWarning toModel(final StockWarningDTO dto) {
        StockWarning model;
        if (dto.isNew()) {
            model = new StockWarning();
        } else {
            model = this.stockWarningRepository.getOne(dto.getId());
        }
        model.setName(dto.getName());
        model.setAmount(dto.getAmount());
        model.setNote(dto.getNote());
        final StringBuffer type = new StringBuffer();
        if ((dto.getNoticeType() != null) && !dto.getNoticeType().isEmpty()) {
            dto.getNoticeType().forEach(t -> {
                type.append(t.trim() + DCAMConsts.COMMA_SPILT);
            });
            model.setNoticeType(type.toString());
        }

        return model;
    }

    @Override
    public StockWarningDTO toDTO(final StockWarning model, final boolean forListView) {
        final StockWarningDTO dto = new StockWarningDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setNote(model.getNote());
        dto.setAmount(model.getAmount());
        if ((model.getAssetTypes() != null) && !model.getAssetTypes().isEmpty()) {
            //            dto.setAssetTypeIds(
            //                    new ArrayList<>(model.getAssetTypes().stream().map(AssetType::getId).collect(Collectors.toList())));
            final String assetTypeNames =
                    model.getAssetTypes().stream().map(AssetType::getName).collect(Collectors.toList()).toString();
            dto.setAssetTypeName(
                    assetTypeNames.substring(1, assetTypeNames.length() - 1));
            dto.setAssetTypeCodes(new ArrayList<>(
                    model.getAssetTypes().stream().map(AssetType::getCode).collect(Collectors.toList())));
        }

        if (!StringUtils.isEmpty(model.getNoticeType())) {
            dto.setNoticeType(new ArrayList<>(Arrays.asList(model.getNoticeType().split(DCAMConsts.COMMA_SPILT))));
            final String noticeTypeNames = dto.getNoticeType().toString();
            dto.setNoticeTypeName(noticeTypeNames.substring(1, noticeTypeNames.length() - 1));
        }
        if (!forListView) {
            dto.setAssetTypes(this.assetTypeConverter.toListDTO(model.getAssetTypes()));
        }
        return dto;
    }
}
